﻿using Microsoft.International.Converters.PinYinConverter;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public static class StringHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="self"></param>
        /// <param name="inStr"></param>
        /// <param name="level">级别 0：模糊程度最高，效率低 1：字符串顺序匹配，效率高</param>
        /// <returns></returns>
        public static bool IsContain(this string self, string inStr, int level = 1)
        {
            string _self = Str2CharString(self).ToLower();
            bool isContain = false;
            if (level == 1)
            {
                if (_self.Contains(inStr) || self.Contains(inStr))
                {
                    isContain = true;
                }
            }
            else if (level == 0)
            {
                string _inStr = Str2CharString(inStr).ToLower();
                char[] compareChars = Str2Chars(_inStr);
                foreach (var item in compareChars)
                {
                    int index = _self.IndexOf(item);
                    if (index != -1)
                    {
                        isContain = true;
                        _self = _self.Substring(index + 1);
                    }
                    else
                    {
                        isContain = false;
                        break;
                    }
                }
            }
            return isContain;
        }

        public static char[] Str2Chars(string str)
        {
            char[] chars = str.ToCharArray();
            string tempStr = "";
            foreach (char c in chars)
            {
                if (ChineseChar.IsValidChar(c))
                {
                    ChineseChar cc = new ChineseChar(c);
                    ReadOnlyCollection<string> roc = cc.Pinyins;
                    tempStr += roc[0].ToLower().Substring(0, 1);
                }
                else
                {
                    tempStr += c;
                }
            }
            return tempStr.ToCharArray();
        }

        public static string Str2CharString(this string str)
        {
            char[] chars = str.ToCharArray();
            string tempStr = "";
            foreach (char c in chars)
            {
                if (ChineseChar.IsValidChar(c))
                {
                    ChineseChar cc = new ChineseChar(c);
                    ReadOnlyCollection<string> roc = cc.Pinyins;
                    tempStr += roc[0].ToLower().Substring(0, 1);
                }
                else
                {
                    tempStr += c;
                }
            }
            return tempStr;
        }

        public static string StringArray2String(this string[] strs)
        {
            string str = "";
            foreach (var item in strs)
            {
                str += item;
            }
            return str;
        }
    }
}
